﻿using System;
using UtfprLibrary.Exceptions;

namespace UtfprLibrary.Helpers {
    /// <summary>
    /// A static helper class for <see cref="AppDomain"/>, that represents an application domain, which is an isolated environment where applications execute.
    /// The notion of process is required to understand what is an application domain.
    /// <para>AppDomain: An application domain is a virtual process that serves to isolate an application. All objects created in the same application scope (in other words, anywhere along the sequence of object activations beginning with the entry point of application) are created in the same application domain. Multiple application domains can exist on a single operating system process, making of them a lightweight application isolation. </para>
    /// <para>Process: An OS process provides isolation through distinct memory space address. While effective, it is also expensive and not scalable to the numbers required for large web server. The common language runtime, on the other hand, reinforces the isolation of application managing the memory usage of the code running in the application domain. This ensures that the code does not access memory outside the domain boundary. It is important to note that only type-safe code can be managed this way (the runtime can not guarantee isolation when unsafe code is loaded into an application domain). </para>
    /// </summary>
    public static class AppDomainHelper {
        /// <summary>
        /// Creates an AppDomain specific for UtfprLibrary.
        /// </summary>
        /// <exception cref="UtfprLibrary.Exceptions.UtfprHelpersException">Thrown when an exception occurs.</exception>
        public static AppDomain CreateDomain(string assemblyPath) {
            try {
                AppDomainSetup domaininfo = new AppDomainSetup();
                domaininfo.ApplicationBase = assemblyPath;
                return AppDomain.CreateDomain(Properties.Resources.Helpers_AppDomainHelper_Const_UtfprLibraryDomain, null, domaininfo);
            } catch (Exception ex) {
                throw new UtfprHelpersException(Properties.Resources.Helpers_AppDomainHelper_ExMsg_CreateDomain, ex);
            }
        }
    }
}